package com.first.computerstore.service;

import com.first.computerstore.dao.AddressMapper;
import com.first.computerstore.dao.OrderMapper;
import com.first.computerstore.dao.UserMapper;
import com.first.computerstore.entity.*;
import com.first.computerstore.query.OrderQuery;
import com.first.computerstore.utils.AssertUtil;
import com.first.computerstore.vo.OrderVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 订单模块的service层
 */
@Service
public class OrderService {

    @Resource
    private OrderMapper orderMapper;

    @Resource
    private AddressMapper addressMapper;

    @Resource
    private ProductService productService;

    @Resource
    private CartService cartService;

    @Resource
    private UserMapper userMapper;



    /**
     * 查询订单列表
     * @param query
     * @return
     */
    public List<OrderVo> queryOrdersByUidAndStatus(OrderQuery query){
        /* 参数校验 */
        Integer uid = query.getUid();
        AssertUtil.isTrue(null == uid,"用户id不能为空!");

        /* 判断查询结果 */
        List<OrderVo> orderList = orderMapper.queryOrdersByUidAndStatus(query);
        AssertUtil.isTrue(null==orderList,"查询订单失败！");

        return orderList;
    }


    /**
     * 订单确认收货
     * @param order
     */
    public void updateOrderStatusByOid(Order order){
        /* 参数校验 */
        AssertUtil.isTrue(null==order.getOid(),"该订单不存在！");
        // 获取状态
        Integer status = order.getStatus();
        /* 设置初始值 */
        if (status == 0) {

        } else if (status == 1) {
            order.setModifiedTime(order.getPayTime());
        }

        /* 判断是否修改成功 */
        AssertUtil.isTrue(orderMapper.updateOrderStatusByOid(order)<1,"修改失败~(>_<)~");

    }


    /**
     * “订单详情”查询
     * @param oid
     * @return
     */
    public List<OrderVo> queryOrderVo(Integer oid) {
        /* 参数校验 */
        AssertUtil.isTrue(null==oid,"订单号不能为空，请选择要查看的订单！");
        Order order = orderMapper.queryOrderByOid(oid);
        AssertUtil.isTrue(null==order,"该订单不存在！");

        List<OrderVo> orderVo = orderMapper.queryOrderVo(oid);

        AssertUtil.isTrue(null==orderVo,"查询订单详情失败！");
        return orderVo;
    }

    /**
     * 通过订单id查询订单对象
     * @param oid
     * @return
     */
    public Order queryOrderByOid(Integer oid) {
        //判断oid是否为空
        AssertUtil.isTrue(null==oid,"订单id不能为空！");
        Order order = orderMapper.queryOrderByOid(oid);
        return order;
    }

    /**
     * 插入订单对象
     * @param aid
     * @param totalPrice
     * @param uid
     * @param userName
     * @return
     */
    public Order insertOrder(Integer aid,Long totalPrice,Integer uid,String userName){
//        获取address对象
        Address address = addressMapper.queryAddressByAid(aid);
//        order对象
        Order order = new Order();
//        补全order对象
        order.setUid(uid);
        order.setAid(aid);
        order.setRecvName(address.getName());
        order.setRecvPhone(address.getPhone());
        order.setRecvProvince(address.getProvinceName());
        order.setRecvCity(address.getCityName());
        order.setRecvArea(address.getAreaName());
        order.setRecvAddress(address.getAddress());
        order.setTotalPrice(totalPrice);
        order.setStatus(0); //表示未支付
        Date createdTime = new Date();
        order.setOrderTime(createdTime);
        order.setPayTime(null);
        order.setCreatedUser(userName);
        order.setModifiedUser(userName);
        order.setCreatedTime(createdTime);
        order.setModifiedTime(createdTime);

//        调用方法，插入数据
        Integer res = orderMapper.insertOneOrder(order);
        AssertUtil.isTrue(res<1,"创建订单失败！");

//        将新插入的订单返回给controller层
        return  orderMapper.queryOrderByOid(order.getOid());
    }

    /**
     * 插入订单商品
     * @param oid
     * @param cid
     * @param num
     * @param userName
     * @return
     */
    public OrderItem insertOrderItem(Integer oid,Integer cid,Integer num,String userName){
//        根据购物车id，获取订单中的商品id
        Cart cart = cartService.queryCartByCid(cid);
        Integer pid = cart.getPid();

//        根据pid获取商品对象
        Product product = productService.queryProductByPid(pid);

//        创建orderItem对象
        OrderItem orderItem = new OrderItem();

        //补全orderItem对象的空白字段
        orderItem.setOid(oid);
        orderItem.setPid(product.getId());
        orderItem.setTitle(product.getTitle());
        orderItem.setImage(product.getImage());
        orderItem.setPrice(product.getPrice());
        orderItem.setNum(num);
        Date createdTime = new Date();
        orderItem.setCreatedUser(userName);
        orderItem.setCreatedTime(createdTime);
        orderItem.setModifiedUser(userName);
        orderItem.setModifiedTime(createdTime);

//        调用持久层插入数据，判断结果
        Integer res = orderMapper.insertOneOrderItem(orderItem);
        AssertUtil.isTrue(res<1,"创建订单失败！");

        return  orderItem;
    }


    public int insertOrderItemFromProductHtml(Integer oid, Integer pid, Integer num, String userName) {
        //根据pid查询商品信息
        Product product = productService.queryProductByPid(pid);

        //创建一个用于向持久层传输的OrderItem实体类对象
        OrderItem orderItem = new OrderItem();

        //补全orderItem对象的空白字段
        orderItem.setOid(oid);
        orderItem.setPid(pid);
        orderItem.setTitle(product.getTitle());
        orderItem.setImage(product.getImage());
        orderItem.setPrice(product.getPrice());
        orderItem.setNum(num);
        Date createdTime = new Date();
        orderItem.setCreatedUser(userName);
        orderItem.setCreatedTime(createdTime);
        orderItem.setModifiedUser(userName);
        orderItem.setModifiedTime(createdTime);

        //调用持久层进行插入
        int result = orderMapper.insertOneOrderItem(orderItem);
        AssertUtil.isTrue(result<1,"创建订单失败！");
        return  result;
    }
}
